/**
 * Project Name:dsrs-systemservice-service
 * File Name:DatadictServiceImpl.java
 * Package Name:com.bwda.dsrs.systemservice.service.impl;
 * Date:2019年4月28日
 * Copyright (c) 2019, 江苏保旺达软件有限公司 All Rights Reserved.
 */
package com.bwda.dsrs.systemservice.service.impl;

import com.bwda.dsrs.base.centerbase.util.PageHelperUtil;
import com.bwda.dsrs.systemservice.domain.Constants;
import com.bwda.dsrs.systemservice.domain.exception.SystemServiceException;
import com.bwda.dsrs.systemservice.domain.po.datadict.DatadictPo;
import com.bwda.dsrs.systemservice.domain.condition.datadict.DatadictCondition;
import com.bwda.dsrs.systemservice.mapper.DatadictMapper;
import com.bwda.dsrs.systemservice.service.DatadictService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 数据字典
 *
 * @author liangjianyong
 * @date 2019/4/8
 */
@Service
public class DatadictServiceImpl extends AbstractBaseServiceImpl<DatadictPo, DatadictCondition, DatadictMapper> implements DatadictService {
    
    private final Logger logger = LoggerFactory.getLogger(DatadictServiceImpl.class);

    @Override
    public int queryCount(DatadictCondition condition) {
        condition.setDelFlag(Constants.NOT_DELETED);
        return super.queryCount(condition);
    }

    @Override
    public List<DatadictPo> queryList(DatadictCondition condition) {
        condition.setDelFlag(Constants.NOT_DELETED);
        return super.queryList(condition);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public int update(DatadictPo po) {
        po.setDelFlag(Constants.NOT_DELETED);
        if(po.getOrderNum()==null){
            //新增时默认排序字段+1
            Integer nextNum =    this.getMapper().queryNextOrder(po.getDataDictTypeCode());
            po.setOrderNum(nextNum);
        }
        return super.update(po);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void updateWithValid(DatadictPo po)
        throws SystemServiceException {
        po.setDelFlag(Constants.NOT_DELETED);
        super.updateWithValid(po);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void insert(DatadictPo po) {
        po.setDelFlag(Constants.NOT_DELETED);
        po.setStatus(Constants.StateType.ENABLE);
        po.setIsDefault(Constants.IS_NOT_DEFAULT);
        if(po.getOrderNum()==null){
            //新增时默认排序字段+1
            Integer nextNum =    this.getMapper().queryNextOrder(po.getDataDictTypeCode());
            po.setOrderNum(nextNum);
        }

        super.insert(po);
    }

    /**
     * 根据ID删除
     *
     * @param id
     * @throws SystemServiceException
     */
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void delete(Object id)
        throws SystemServiceException {
        DatadictPo po = new DatadictPo();
        po.setId((Integer)id);
        po.setDelFlag(Constants.DELETED);
        this.getMapper().update(po);
    }

    /**
     * 根据code查询数据字典值
     * 
     * @param dataDictCode
     * @return
     * @throws SystemServiceException
     */
    @Override
    public String getDataDictValByCode(String dataDictCode)
        throws SystemServiceException {
        DatadictCondition datadictCondition = new DatadictCondition();
        datadictCondition.setDelFlag(Constants.NOT_DELETED);
        datadictCondition.setStatus(Constants.ENABLE);
        datadictCondition.setDataDictCode(dataDictCode);
        List<DatadictPo> datas = this.getMapper().selectList(datadictCondition);
        if (datas == null || datas.size() == 0) {
            throw new SystemServiceException(dataDictCode + "对应的记录为空");
        }else if (datas.size() > 1) {
            logger.error("dataDictCode:{} 对应{}条记录", dataDictCode, datas.size());
            throw new SystemServiceException(dataDictCode + "对应的记录数据有误");
        }else {
            return datas.get(0).getDataDictValue();
        }
    }

    @Override
    public List<DatadictPo> getDataDictValByTypeCode(String dataDictTypeCode) throws SystemServiceException {
        DatadictCondition datadictCondition = new DatadictCondition();
        datadictCondition.setDelFlag(Constants.NOT_DELETED);
        datadictCondition.setStatus(Constants.ENABLE);
        datadictCondition.setDataDictTypeCode(dataDictTypeCode);
        List<DatadictPo> datas = this.getMapper().selectList(datadictCondition);
        if (datas == null || datas.size() == 0) {
            throw new SystemServiceException(dataDictTypeCode + "对应的记录为空");
        } else {
            return datas;
        }
    }

    /**
     * 批量新增
     * 
     * @param datas
     */
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void insertBatch(List<DatadictPo> datas) {
        this.getMapper().insertBatch(datas);
    }
    
    /**
     * 根据类型code 查询未删除且正常的字典列表
     * 
     * @param typeCode 根据类型code查询字典列表
     * @return
     */
    @Override
    public List<DatadictPo> selectValidListByListCode(String typeCode) {
        DatadictCondition condition = new DatadictCondition();
        condition.setDataDictTypeCode(typeCode);
        condition.setDelFlag(Constants.NOT_DELETED);
        condition.setStatus(Constants.ENABLE);
        return this.getMapper().selectList(condition);
    }
    
    /**
     * 查询数量（返回带dataDicTypeName）
     * 
     * @param condition 可以根据类型名称模糊查询
     * @return
     */
    @Override
    public int queryCountWithTypeName(DatadictCondition condition) {
        PageHelperUtil.startPage(0, 0);
        condition.setDelFlag(Constants.NOT_DELETED);
        return this.getMapper().countWithTypeName(condition);
    }
    
    /**
     * 查询（返回带dataDicTypeName）
     *
     * @param condition
     * @return
     */
    @Override
    public List<DatadictPo> queryListWithTypeName(DatadictCondition condition) {
        PageHelperUtil.startPage(condition.getPageNum(), condition.getPageSize());
        condition.setDelFlag(Constants.NOT_DELETED);
        return this.getMapper().selectListWithTypeName(condition);
    }

    /**
     * 批量删除
     * @param datadictIdList
     */
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void batchDelete(List<Integer> datadictIdList) {
        DatadictPo po=new DatadictPo();
        po.setIdList(datadictIdList);
        po.setDelFlag(Constants.DELETED);
        this.getMapper().update(po);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void batchDeleteByTypeCodeList(List<String> datadicTypeCodeList) {
        DatadictPo po=new DatadictPo();
        po.setDataDictTypeCodeList(datadicTypeCodeList);
        po.setDelFlag(Constants.DELETED);
        this.getMapper().update(po);
    }

    /**
     * 禁用此类型下所有数据
     * @param datadicTypeCode
     */
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void updateStatusByTypeCode(String datadicTypeCode){
        this.getMapper().updateStatusByTypeCode(datadicTypeCode);
    }

    @Override
    public Integer selectMaxOrderNum() {
        return this.getMapper().selectMaxOrderNum();
    }

    @Override
    public int selectCountByType(String datadicTypeCode) {
        return this.getMapper().selectCountByType(datadicTypeCode);
    }


}